package com.example.udpNetThread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

/**
 * @Author 郅占伟
 * @ClassName SendData
 * @Description TODO
 * @Date 2024年05月15日
 * @Version: 1.0
 */
public class SendData extends Thread {
    @Override
    public void run() {
        try {
            // 创建发送端Socket对象
            DatagramSocket datagramSocket = new DatagramSocket();

            // 封装键盘输入
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

            // 创建数据并打包
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                byte[] bytes = line.getBytes();
                DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 10086);
                datagramSocket.send(datagramPacket);

                if ("bye".equals(line)) {
                    bufferedReader.close();
                    datagramSocket.close();
                    break;
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
